24. Display Info

Displaying Information to the Screen

16# Example - Display Info Part 1

16# Example - Display Info Part 2

Data

Data retrieval is done in two steps.

  1. Initialize the SystemParser object, which contains the data for the host machine attributes. This object contains data about RAM usage, overall CPU usage, the number of total processes, the thread count, and the number of running processes. Besides that, the object stores the kernel version and the host OS version and the total machine up time.
  2. Retrieve process-specific data. This includes information about the owner of each process, the total CPU usage of that process, RAM percentage, the execution command, and the unique process id (PID).

main.cpp

Display

Representation of the data is done using the ncurses library. ncurses is a dedicated API for writing terminal-independent text outputs. It refreshes the relevant data every second. ncurses also creates percentage bars and separate windows within the terminal.

void writeSysInfoToConsole(SysInfo sys, WINDOW* sys_win)
{
    sys.setAttributes();

    mvwprintw(sys_win,2,2,getCString(( "OS: " + sys.getOSName())));
    mvwprintw(sys_win,3,2,getCString(( "Kernel version: " + sys.getKernelVersion())));
    mvwprintw(sys_win,4,2,getCString( "CPU: "));
    wattron(sys_win,COLOR_PAIR(1));
    wprintw(sys_win,getCString(Util::getProgressBar(sys.getCpuPercent())));
    wattroff(sys_win,COLOR_PAIR(1));
    mvwprintw(sys_win,5,2,getCString(( "Other cores:")));
    wattron(sys_win,COLOR_PAIR(1));
    std::vector<std::string> val = sys.getCoresStats();
    for (int i = 0; i < val.size(); i++) {
        mvwprintw(sys_win,(6+i),2,getCString(val[i]));
    }
    wattroff(sys_win,COLOR_PAIR(1));
    mvwprintw(sys_win,10,2,getCString(( "Memory: ")));
    wattron(sys_win,COLOR_PAIR(1));
    wprintw(sys_win,getCString(Util::getProgressBar(sys.getMemPercent())));
    wattroff(sys_win,COLOR_PAIR(1));
    mvwprintw(sys_win,11,2,getCString(( "Total Processes:" + sys.getTotalProc())));
    mvwprintw(sys_win,12,2,getCString(( "Running Processes:" + sys.getRunningProc())));
    mvwprintw(sys_win,13,2,getCString(( "Up Time: " + Util::convertToTime(sys.getUpTime()))));

}

WriteSysInfoToConsole is a function with two input parameters. The first parameter is of SysInfo type. The second parameter is a dedicated ncurses object pointer, WINDOW*. This window parameter specifies part of the terminal reserved for the system data.

getProcessListToConsole()

This function prints the first 10 processes from the host machine, which are stored in a string vector. They are distributed across 10 rows in a dedicated window. Every invocation of the function refreshes the process list and prints refreshed datas.

The function prototype has two input parameters: the ProcessContainer object, which is a container wrapper around the Process object, and the pointer WINDOW type from ncurses.

void getProcessListToConsole(ProcessContainer procs,WINDOW* win)
{
    procs.refreshList();
    wattron(win,COLOR_PAIR(2));
    mvwprintw(win,1,2,"PID:");
    mvwprintw(win,1,9,"User:");
    mvwprintw(win,1,16,"CPU[%]:");
    mvwprintw(win,1,26,"RAM[MB]:");
    mvwprintw(win,1,35,"Uptime:");
    mvwprintw(win,1,44,"CMD:");
    wattroff(win, COLOR_PAIR(2));
    vector<std::string> processes = procs.getList();
    for(int i = 0; i < 10; i++) {
        mvwprintw(win,2 +i,2,getCString(processes[i]));
    }
}

printMain()

The binding function for consistent display of data via ncurses is the printMain() function. This function accepts two parameters: a SysInfo object and a ProcessContainer. The function initializes the ncurses screen, separate windows, and a coloring scheme.

The infinite loop is retrieves data and updates the terminal screen. With this technique we achieve a live display of the machine state.

Every window is separately refreshed via wrefresh, and boundaries are set for each window via the box method. These are ncurses methods.

void printMain(SysInfo sys,ProcessContainer procs)
{
    initscr(); // start curses mode
    noecho(); // not printing input values
    cbreak(); // Terminating on classic ctrl + c
    startColor(); // Enabling color change of text
    int yMax,xMax;
    getmaxyx(stdscr,yMax,xMax); // getting size of window measured in lines and columns(column one char length)
    WINDOW *sys_win = newwin(17,xMax-1,0,0);
    WINDOW *proc_win = newwin(15,xMax-1,18,0);

    init_pair(1,COLOR_BLUE,COLOR_BLACK);
    init_pair(2,COLOR_GREEN,COLOR_BLACK);

    while (true) {
        box(sys_win,0,0);
        box (proc_win,0,0);
        writeSysInfoToConsole(sys,sys_win);
        getProcessListToConsole(procs,proc_win);
        wrefresh(sys_win);
        wrefresh(proc_win);
        refresh();
        sleep(1);
    }
    endwin();
}

main()

main() initializes the ProcessContainer and SysInfo objects. After that we directly invoke printMain().

int main( int   argc, char *argv[] )
{
     //Object which contains list of current processes, Container for Process Class
    ProcessContainer procs;
    // Object which containts relevant methods and attributes regarding system details
    SysInfo sys;
    //string s = writeToConsole(sys);
    printMain(sys,procs);
    return 0;
}